/*
 * @lc app=leetcode.cn id=203 lang=java
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while (head!=null&&head.val==val) {
            head=head.next;
        }
        if (head==null) {
            return head;
        }
        ListNode middle=head,middle2=head.next;
        while (middle2!=null) {
            if (middle2.val==val) {
                middle2=middle2.next;
                middle.next=middle2;
            }else{
                middle=middle.next;
                middle2=middle2.next;
            }
        }
        return head;
    }
}
// @lc code=end

